package com.yc.jpyy.mobile.controller;

import java.util.List;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.etrans.ecpm.impl.RdtIntfWebImpl;
import com.etrans.ecpm.intf.IRdtIntf;
import com.etrans.ecpm.intf.TCmdReply;
import com.etrans.ecpm.intf.TCmdSendResp;
import com.etrans.ecpm.intf.TCmdStepInfo;
import com.etrans.ecpm.intf.TIntfObjAlarm;
import com.etrans.ecpm.intf.TIntfObjImage;
import com.etrans.ecpm.intf.TIntfObjTrack;
import com.etrans.ecpm.intf.UReqObj;

public class Demo {

	private final static Logger logger = LoggerFactory.getLogger(Demo.class);


    //final IRdtIntf intf = RdtIntfFactory.getRdtIntf();
	final IRdtIntf intf = new RdtIntfWebImpl();
    Demo() {
        String host = "27.188.64.102";
        try {
			intf.setup(
			        host+":16389:foobared",
			        host+":16379:foobared",
			        host+":16389:foobared",
			        host+":16379:foobared"
			);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    @SuppressWarnings("unused")
	private void loopTrack() {
        while (true) {
            try {
                List<TIntfObjTrack> resTrack = intf.getTrack(new String[]{"013287310241"});
                List<TIntfObjAlarm> resAlarm = intf.getAlarm((String[]) null);

                if (null != resTrack) {
                    for (TIntfObjTrack objTrack : resTrack) {
                        logger.info("objTrack: {}", objTrack);
                    }
                }

                if (null != resAlarm) {
                    for (TIntfObjAlarm objAlarm : resAlarm) {
                        logger.info("objAlarm: {}", objAlarm);
                    }
                }

                Thread.sleep(10 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] _args) throws Exception {
    	try{
        final Demo demo = new Demo();
        final IRdtIntf intf = demo.intf;

//        Set<Integer> alarmkinds = new HashSet<>();
//        alarmkinds.add(1);
//        alarmkinds.add(2);
//        alarmkinds.add(3);
//
//        intf.clearAlarm( "013287310996");
//        logger.info("{}", tracks);
//
//        for (int i = 0; i < CODES.length; i++) {
//            TIntfObjTrack t = tracks.get(i);
//            logger.info("CODE={} RES={}", CODES[i], null != t ? t.getCommNo() : "NULL");
//        }

//        Thread.sleep(Integer.MAX_VALUE);

//        new Thread(demo::loopTrack).start();

        // 拍照
        String commno = "100900010001";
        int vehicle_id = 92545;
        int terminal_id = 105192;
        int terminalType = 950;

        int flag = 1; 

        int cmdId;
        String args;

        if (flag == 1) {
            cmdId = 0x8300;
            args = "2,0,0,1,0,abc";
        } else if (flag == 2) {
            cmdId = 0x8801;
            args = "1,1,600,0,2,1,100,100,100,100";
        }else if (flag == 3){
            cmdId = 0xA101;
            //args = "http://127.0.0.1:8081/123456789";
            args = "http://27.188.64.102:8020/1234567891011121314";
        }else{
            cmdId = 0xA110;
            args = "0000000000";
        }

        long seq = 0;
        while (true) {
            UUID uuid = UUID.randomUUID();

            long send_time = System.currentTimeMillis();
            UReqObj req_obj = new UReqObj(uuid, send_time, 197, vehicle_id, terminal_id, terminalType, commno, cmdId, args);
            logger.info("请求下发指令 {} {}", ++seq, req_obj);
            TCmdSendResp resp = intf.sendCMD(req_obj);

            logger.info("send cmd [{}]", uuid.toString());
            if (null == resp) {
                logger.error("not get response.");
                return;
            }

            logger.info("cmdResp={}, hint={}", resp.getReqUUID().toString(), resp.getRespType().getName());

            TCmdStepInfo stepInfo;

            stepInfo = intf.getCmdStepInfo(uuid, 5);
            logger.info("stepInfo={}", stepInfo);

            TCmdReply reply = null;
            if (stepInfo.hasReply()) {
                logger.info("等待 终端 应答");
                reply = intf.getReply(terminalType, commno, cmdId, resp.getCmdPkgSeq(), 40);
                if (null == reply)
                    logger.info("等待终端应答超时");
            } else {
                logger.info("没收到回复");
            }

            if (null != reply) {
                logger.info("cmdReply: {}, {}", reply.getReplyBody(), reply.getReplyMsg());

                if (flag == 2 && reply.isCmdOK()) {
                    TIntfObjImage image = intf.getImage(commno, terminal_id, 1, send_time, 600);
                    if (null != image) {
                        logger.info("{}", image.getImageUrl());
                    } else {
                        logger.info("image is null");
                    }
                }
            }

            Thread.sleep(1000 * 60);
        }}catch (Exception e){
    		System.out.print(e.toString());
    	}
    }
    	

    final static String[] CODES = new String[]{
            "013681402542",
            "013690010936",
            "013681402729",
            "013690006888",
            "013181415349",
            "013690035897",
            "013690023992",
            "013287311021",
            "013690006540",
            "013690021731",
            "013690008829",
            "013690008335",
            "013181059768",
            "013690009071",
            "013710107749",
            "013287311993",
            "013681010879",
            "013681011270",
            "013287311974",
            "013681011255",
            "013681400478",
            "013220605873",
            "013681400480",
            "013220605934",
            "013681402564",
            "013287312106",
            "013681409931",
            "013690034230",
            "013681407497",
            "013681010912",
            "013681407463"
    };
}
